using ETask.Api.Dtos;
using ETask.Api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ETask.Api.Controllers
{
    /// <summary>
    /// 审计日志控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class AuditLogController : ControllerBase
    {
        private readonly IAuditLogService _auditLogService;

        public AuditLogController(IAuditLogService auditLogService)
        {
            _auditLogService = auditLogService;
        }

        /// <summary>
        /// 分页查询审计日志
        /// </summary>
        /// <param name="queryParams">查询参数</param>
        /// <returns>分页结果</returns>
        [HttpPost("getList")]
        public async Task<IActionResult> GetList([FromBody] AuditLogQueryParams queryParams)
        {
            try
            {
                var result = await _auditLogService.GetPagedListAsync(queryParams);
                return Ok(ApiResponse.Ok(result));
            }
            catch (Exception ex)
            {
                return Ok(ApiResponse.Fail(500, $"查询审计日志失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 获取用户最近的日志
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="count">数量，默认10条</param>
        /// <returns>日志列表</returns>
        [HttpPost("getRecentLogs")]
        public async Task<IActionResult> GetRecentLogs([FromBody] PrimaryKey key)
        {
            try
            {
                var result = await _auditLogService.GetRecentLogsByUserIdAsync(key.Id, 10);
                return Ok(ApiResponse.Ok(result));
            }
            catch (Exception ex)
            {
                return Ok(ApiResponse.Fail(500, $"获取用户日志失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 获取操作类型列表
        /// </summary>
        /// <returns>操作类型列表</returns>
        [HttpGet("getActionTypes")]
        public IActionResult GetActionTypes()
        {
            var actionTypes = new[]
            {
                new { Value = "登录", Label = "登录" },
                new { Value = "登出", Label = "登出" },
                new { Value = "创建", Label = "创建" },
                new { Value = "更新", Label = "更新" },
                new { Value = "删除", Label = "删除" },
                new { Value = "添加", Label = "添加" },
                new { Value = "修改", Label = "修改" },
                new { Value = "重置密码", Label = "重置密码" },
                new { Value = "上传", Label = "上传" },
                new { Value = "下载", Label = "下载" }
            };

            return Ok(ApiResponse.Ok(actionTypes));
        }

        /// <summary>
        /// 获取模块列表
        /// </summary>
        /// <returns>模块列表</returns>
        [HttpGet("getModules")]
        public IActionResult GetModules()
        {
            var modules = new[]
            {
                new { Value = "用户", Label = "用户" },
                new { Value = "部门", Label = "部门" },
                new { Value = "项目", Label = "项目" },
                new { Value = "项目成员", Label = "项目成员" },
                new { Value = "项目文档", Label = "项目文档" },
                new { Value = "项目任务", Label = "项目任务" },
                new { Value = "任务文件", Label = "任务文件" },
                new { Value = "系统", Label = "系统" }
            };

            return Ok(ApiResponse.Ok(modules));
        }
    }
}
